<template>
	<view>
		<uni-forms ref="form" :model="user" :rules="rules" style="width: 90%;margin-left: 5%;padding-top: 5%;">
			<uni-forms-item label="用户名" name="name" required>
				<uni-easyinput v-model="user.name" placeholder="请输入用户名" />
			</uni-forms-item>
			<uni-forms-item label="密码" name="password" required>
				<uni-easyinput type="password" v-model="user.password" placeholder="请输入密码" />
			</uni-forms-item>
			<uni-forms-item label="邮箱" name="email" required>
				<uni-easyinput v-model="user.email" placeholder="请输入邮箱" />
			</uni-forms-item>
			<uni-forms-item label="电话" name="telephone">
				<uni-easyinput v-model="user.telephone" placeholder="请输入电话" />
			</uni-forms-item>
			<uni-forms-item label="学院" name="department">
				<uni-data-select v-model="user.department" :localdata="department" placeholder="请选择学院" :clear="false" />
			</uni-forms-item>
			<uni-forms-item label="专业" name="major">
				<uni-data-select v-model="user.major" :localdata="major" placeholder="请选择专业" :clear="false" />
			</uni-forms-item>
			<uni-forms-item label="" name="">
				<button type="primary" @click="register">注册</button>
			</uni-forms-item>
		</uni-forms>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {
					name: '',
					password: '',
					email: '',
					telephone: '',
					department: '',
					major: ''
				},
				department: [{
						value: '0',
						text: '篮球'
					},
					{
						value: '1',
						text: 'Music'
					},
					{
						value: '2',
						text: '背带裤'
					}
				],
				major: [{
						value: '0',
						text: '唱'
					},
					{
						value: '1',
						text: '跳'
					},
					{
						value: '2',
						text: 'Rap'
					}
				],
				rules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '用户名不能为空'
						}]
					},
					password: {
						rules: [{
							required: true,
							errorMessage: '密码不能为空'
						}, {
							minLength: 3,
							maxLength: 5,
							errorMessage: '密码的长度要在{minLength}和{maxLength}之间'
						}, {
							pattern: '\\d',
							errorMessage: '密码至少包含一个数字'
						}]
					},
					email: {
						rules: [{
							required: true,
							errorMessage: '邮箱不能为空'
						}]
					}
				}
			};
		},
		methods: {
			register() {
				this.$refs.form.validate()
					.then((res) => {
						uni.request({
							url: 'http://localhost:8090/freshmenapp/user/register',
							method: 'POST',
							data: this.user,
							success: res => {
								if (res.data.success) {
									uni.showToast({
										title: '注册成功'
									});
									uni.navigateTo({
										url: 'pages/login/login'
									});
								} else {
									uni.showToast({
										title: '网络异常',
										icon: 'error'
									});
								}
							},
							fail: (error) => {
								uni.showToast({
									icon: 'error',
									title: '注册失败'
								});
							}
						});
					}).catch((error) => {
						uni.showToast({
							icon: 'error',
							title: '请填写完整信息'
						});
					});
			}
		}
	}
</script>

<style>

</style>